#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

import time

from column import Column
from supercolumn import SuperColumn
from columnfamily import ColumnFamily

from row import Row


COLUMN_FAMILY_SEPARATOR = ':'

class   RowMutator(object) :
    def __init__(self, table, key) :
        self.table = table
        self.row_key = key

        # map< column_family_name, column_family_object >
        self._modifications = {}
        ## __init__()

    def __del__(self) :
        self._modifications = {}
        ## __del__()

    @staticmethod
    def splitColumnAndColumnFamilyName(column_family_name) :
        return  column_family_name.split( COLUMN_FAMILY_SEPARATOR )
        ## splitColumnAndColumnFamilyName()

    def addColumnFamily(self, column_family) :
        cfname = column_family.name
        if cfname in self._modifications :
            raise RuntimeError, 'ColumnFamily "%s" is already being modified.' % cfname
        self._modifications[ cfname ] = column_family
        ## add()

    def delete(self, query_path, timestamp) :
        cfname = query_path.column_family_name
        if cfname in self._modifications :
            raise RuntimeError, 'ColumnFamily "%s" is already being modified.' % cfname

        local_delete_time = int(time.time() * 1000)

        column_family = ColumnFamily.create(self.table.name, cfname)

        if not query_path.super_column_name and not query_path.column_name :
            # delete column-family
            column_family.delete(local_delete_time, timestamp)
        elif not query_path.column_name :
            # delete super-column
            sc = SuperColumn(query_path.super_column_name)
            sc.markForDeleteAt(local_delete_time, timestamp)
            column_family.addColumn(sc)
        else :
            # delete column
            column = Column(query_path.column_name, str(local_delete_time), timestamp, True)
            if query_path.super_column :
                sc = SuperColumn(query_path.super_column_name)
                sc.addColumn(column)
                column = sc
            column_family.addColumn(column)

        self._modifications[ cfname ] = column_family
        ## delete()

    def apply(self) :
        row = Row(self.table.name, self.row_key)

        for cfname,cf in self._modifications.iteritems() :
            row.addColumnFamily( cf )

        self.table.apply(row)
        ## apply()

    def __str__(self) :
        return '<RowMutator table:%s key:%s>' % (self.table.name, self.row_key)
        ## __str__()

    ## class RowMutator()

